{include file="common/head" /}
  <body>
    <div class="x-body" >
        <form class="layui-form">
          <div class="layui-form-item">
              <label for="L_name" class="layui-form-label">
                  <span class="x-red">*</span>分类名称
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="L_name" name="name" required=""
                  autocomplete="off" class="layui-input">
              </div>
          </div>

            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>图片
                </label>
                <a class="layui-btn" id="image" style="float: left;"><i class="layui-icon">&#xe67c;</i>上传图片</a>
            </div>
            <div class="layui-form-item">
            <div class="layui-input-inline">
                <img src="" id="thumb" alt="" width="400" height="200" style="margin-left: 110px;display: none" >
                <input type="hidden" name="url" value="">
            </div>
            </div>
            <div class="layui-form-item">
                <label for="L_description" class="layui-form-label">
                    描述
                </label>
                <div class="layui-input-block">
                    <textarea name="description" placeholder="请输入内容" id="L_description" class="layui-textarea"></textarea>
                </div>
            </div>
          <div class="layui-form-item">
              <label  class="layui-form-label">
              </label>
              <button  class="layui-btn" lay-filter="add" lay-submit="">
                  增加
              </button>
          </div>
      </form>
    </div>
    <script>
        layui.use(['form','layer'], function(){
            $ = layui.jquery;
          var form = layui.form
          ,layer = layui.layer;
        
          //自定义验证规则
          form.verify({
            nikename: function(value){
              if(value.length < 5){
                return '昵称至少得5个字符啊';
              }
            }
            ,pass: [/(.+){6,12}$/, '密码必须6到12位']
            ,repass: function(value){
                if($('#L_pass').val()!=$('#L_repass').val()){
                    return '两次密码不一致';
                }
            }
          });

          //监听提交
          form.on('submit(add)', function(data){
            console.log(data.field);
            $.post('/admin/category_add',{
                name:data.field.name,
                url:data.field.url,
                description:data.field.description
            },function (ret) {
                if(ret.code == 1){
                    layer.alert(ret.msg, {icon: 6},function () {
                        // 获得frame索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭当前frame
                        parent.layer.close(index);
                    });
                }else{
                    layer.alert(ret.msg, {icon: 5});
                }
            });
            return false;
          });
          
          
        });
    </script>
    <script>
        layui.use('upload',function(){
            var upload = layui.upload,
                jq = layui.jquery;
            upload.render({
                url: '{:url("admin/upload")}'
                ,elem:'#image'
                ,ext: 'jpg|png|gif'
                ,area: ['200', '200px']
                ,before: function(input){
                    loading = layer.load(2, {
                        shade: [0.2,'#000']
                    });
                }
                ,done: function(res){
                    layer.close(loading);
                    $("#thumb").attr('src','/images/'+res.saveName);
                    $("input[name=url]").val(res.saveName);
                    $("#thumb").css('display','block');
                    layer.msg(res.msg, {icon: 1, time: 1000});
                }
            });

        })
    </script>
  </body>

</html>